package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.base.service.impl.BaseServiceImpl;
import com.example.common.utils.ApiResult;
import com.example.dao.*;
import com.example.model.entity.*;
import com.example.model.query.TSourceQuery;
import com.example.model.query.TWarningQuery;
import com.example.service.TWarningService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 告警上报记录表 Service接口实现类
 *
 * @author zhangr
 * @Description Created on 2020-12-22
 */
@Service
public class TWarningServiceImpl extends BaseServiceImpl<TWarningMapper,TWarning> implements TWarningService {

    @Autowired
    private TWarningMapper warningMapper;

    @Autowired
    private TEquipmentMapper equipmentMapper;

    @Autowired
    private TEquipmentPartMapper equipmentPartMapper;

    @Autowired
    private TSourceMapper sourceMapper;

    @Autowired
    private TPlanMapper planMapper;

    @Override
    public ApiResult<TWarningQuery> pageUserVoByQuery(TWarningQuery query) {
        query.setOptimizeCountSql(false);
        Long current = query.getCurrent();
        query.setCurrent(1L);
        Integer total = warningMapper.countByQuery(query);
        query.setTotal(total);
        query.setCurrent(current);
        warningMapper.pageInfoByQuery(query);
        List list = query.getRecords();
        for (int i = 0; i < list.size(); i++) {
            TWarningQuery warningQuery = (TWarningQuery) list.get(i);
            if(StringUtils.isNotBlank(warningQuery.getEquipmentId())){
                TEquipment equipment = equipmentMapper.selectById(warningQuery.getEquipmentId());
                if(equipment != null){
                    warningQuery.setEquipment(equipment);
                    warningQuery.setEquipmentName(equipment.getEquipmentName());
                }
            }
            if(StringUtils.isNotBlank(warningQuery.getPartId())){
                TEquipmentPart part = equipmentPartMapper.selectById(warningQuery.getPartId());
                if(part != null){
                    warningQuery.setEquipmentPart(part);
                    warningQuery.setPartName(part.getPartName());
                    if(StringUtils.isNotBlank(part.getSourceId())){
                        TSource source = sourceMapper.selectById(part.getSourceId());
                        if(source != null){
                            warningQuery.setSourceName(source.getSourceName());
                        }
                    }
                    //获取配置预案名称
                    List<TPlan> planList = planMapper.getPlanByPartId(part.getPartId());
                    warningQuery.setPlanList(planList);
                }
            }
        }
        return new ApiResult<>(query);
    }
}

